package com.tyros.web.log.aspect;

import com.tyros.common.kit.json.JacksonKit;
import com.tyros.common.kit.spring.HttpContextKit;
import com.tyros.web.log.HandlerLogProperties;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhangshuaiyin
 * @date 2022/7/2 12:01
 */
@Slf4j
public class AspectLogHelper {
    public static Object successLog(ProceedingJoinPoint joinPoint, @Nullable ApiOperation apiOperation, HandlerLogProperties properties) throws Throwable {
        if (!properties.isEnable()) {
            return joinPoint.proceed();
        }

        // StopWatch stopWatch = StopWatch.createStarted();
        // stopWatch.start();
        long start = System.currentTimeMillis();
        // 执行切点方法
        Object result = joinPoint.proceed();
        HttpServletRequest request = HttpContextKit.getRequest();
        assert request != null;
        String content = "";
        content += "\n========================== Request Info ==========================";
        content += "\nExecute Method : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + "()";
        content += "\nRequest API    : " + request.getRequestURI();
        content += "\nHTTP Method    : " + request.getMethod();
        if (apiOperation != null) {
            content += "\nAPI Description: " + apiOperation.value();
        }
        content += "\nRequest Params : " + JacksonKit.toJson(joinPoint.getArgs());
        content += "\nIP Address     : " + request.getRemoteAddr();
        content += "\nReturn Result  : " + result;
        content += "\nTotal Mills    : " + (System.currentTimeMillis() - start) + "ms";
        content += "\n========================== Request  END ==========================";
        log.info(content);
        return result;
    }

    public static void throwingLog(Throwable ex, @Nullable ApiOperation apiOperation, HandlerLogProperties properties) {
        if (!properties.isEnable()) {
            return ;
        }
        HttpServletRequest request = HttpContextKit.getRequest();
        assert request != null;
        String content = "";
        content += "\n========================== Request Error Info ==========================";
        content += "\nRequest API    : " + request.getRequestURI();
        content += "\nHTTP Method    : " + request.getMethod();
        if (apiOperation != null) {
            content += "\nAPI Description: " + apiOperation.value();
        }
        content += "\nIP Address     : " + request.getRemoteAddr();
        content += "\nThrowable      : " + ExceptionUtils.getMessage(ex);
        content += "\n========================== Request Error END ===========================";
        log.error(content);
    }
}
